//
//  TravellerViewController.m
//  Journey
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravellerViewController.h"

#import "SSNetworking.h"
#import "TimesModel.h"
#import "DestinModel.h"
#import "PlaceModel.h"
#import "TravellerTableViewCell.h"
#import "TravellerModel.h"
#import "TimerCollectionViewCell.h"
#import "YNDataReading.h"
static NSString *const TravellerCell = @"TravellerCELL";

@interface TravellerViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UITableView *traTableView;

@property(nonatomic,strong)NSMutableArray *timersArray;
@property(nonatomic,strong)NSMutableArray *destinationArray;
@property(nonatomic,strong)NSMutableArray *placeArray;
@property(nonatomic,strong)NSMutableArray *tempArray;
@property(nonatomic,strong)NSMutableArray *travellerArray;

@property(nonatomic,assign)NSInteger nextPage;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,strong)NSMutableArray *imageArray;

@property(nonatomic,strong)BaseCollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *end_time;
@property(nonatomic,strong)NSMutableArray *start_time;

@end

@implementation TravellerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFFFF,0x363636);
    
       //初始化数组
    self.timersArray = [NSMutableArray array];
    self.destinationArray = [NSMutableArray array];
    self.placeArray = [NSMutableArray array];
    self.tempArray = [NSMutableArray array];
    self.travellerArray = [NSMutableArray array];
    
    self.end_time = [NSMutableArray array];
    self.start_time = [NSMutableArray array];
    
     //加载条
    //显示
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    //  如果是有导航栏请清除自动适应设置
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.traTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40 * WIDTHC, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.traTableView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.traTableView];
    self.traTableView.dataSource = self;
    self.traTableView.delegate = self;
    
    self.traTableView.showsVerticalScrollIndicator = NO;
    [self.traTableView registerClass:[TravellerTableViewCell class] forCellReuseIdentifier:TravellerCell];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(80 * WIDTHC,35 * WIDTHC);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 10;
  //  flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40) collectionViewLayout:flowLayout];
    //取消水平滚动条
  self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.backgroundColor =[UIColor clearColor];
   
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    
    [self.collectionView registerClass:[TimerCollectionViewCell class] forCellWithReuseIdentifier:@"discountCell"];
    [self.view addSubview:self.collectionView];
     [self getData];
        
    self.imageArray = [NSMutableArray array];
    for (int i = 1; i < 4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%d",i]];
        [self.imageArray addObject:image];
    }
    
    self.isUpLoading = NO;
    [self addFooter];
    [self addHeader];

    
}


-(void)addHeader{
    
    __block TravellerViewController *travellerVC = self;
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        travellerVC.nextPage = 1;
        travellerVC.isUpLoading = NO;
        
        [travellerVC getDetailDataList:travellerVC.nextPage];
        
       // [MBProgressHUD showHUDAddedTo:travellerVC.view animated:YES];
    }];
    
    //普通闲置状态
    [header setImages:self.imageArray forState:MJRefreshStateIdle];
    //松开就可以进行刷新的状态
    [header setImages:self.imageArray forState:MJRefreshStatePulling];
    //正在刷新中的状态
    [header setImages:self.imageArray forState:MJRefreshStateRefreshing];
    
    self.traTableView.mj_header = header;
    [self.traTableView.mj_header beginRefreshing];
}

-(void)addFooter{
    __block TravellerViewController *travellerVC = self;
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        
        travellerVC.nextPage += 20;
        travellerVC.isUpLoading = YES;
        [travellerVC getDetailDataList:travellerVC.nextPage];
       
        
    }];
    
    [footer setImages:self.imageArray forState:MJRefreshStateRefreshing];
    self.traTableView.mj_footer = footer;
}

-(void)getData{
    [SSNetworking ssGETWithURL:@"http://open.qyer.com/qyer/community/company/filter?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88283798007659&lon=121.539156053574&oauth_token=4ea07b0d41e13f5cb1448a45a3613e45&page=1&track_app_channel=App%2520Store&track_app_version=6.9.0&track_device_info=iPhone8%2C1&track_deviceid=F417850E-117E-4BF4-AAA6-3AD14E14E3C1&track_os=ios%25209.3.1&track_user_id=7891151&v=1" parameter:nil success:^(id result) {
        
        [[YNDataReading shareYNataReading]writeCachePlist:@"timeData" result:result ];
        
        NSDictionary *bigDic = [[YNDataReading shareYNataReading]readCachePlist:@"timeData" ];
        NSDictionary *dataDic = [bigDic objectForKey:@"data"];
        
       NSMutableArray *timesArray = [dataDic objectForKey:@"times_range"];
        
        for (NSDictionary *dic in timesArray) {
            TimesModel *times = [[TimesModel alloc] init];
            [times setValuesForKeysWithDictionary:dic];
            [self.timersArray addObject:times.myDescrip];
            
           
        }
       
      [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        
    } network:^(BOOL networking) {
        
       
        NSDictionary *bigDic = [[YNDataReading shareYNataReading]readCachePlist:@"timeData" ];
        NSDictionary *dataDic = [bigDic objectForKey:@"data"];
        
        NSMutableArray *timesArray = [dataDic objectForKey:@"times_range"];
        
        for (NSDictionary *dic in timesArray) {
            TimesModel *times = [[TimesModel alloc] init];
            [times setValuesForKeysWithDictionary:dic];
            [self.timersArray addObject:times.myDescrip];
            
            
        }
        
        [self.collectionView reloadData];
    }];
 }
-(void)getDetailDataList:(NSInteger)nextPage{
    
    NSString *string = @"2520";
    NSString *str = @"2";
    NSString *str1 = @"25209.3.1";
    
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/company/search_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88283798007659&lon=121.539156053574&oauth_token=4ea07b0d41e13f5cb1448a45a3613e45&page=%ld&track_app_channel=App%@Store&track_app_version=6.9.0&track_device_info=iPhone8%@C1&track_deviceid=F417850E-117E-4BF4-AAA6-3AD14E14E3C1&track_os=ios%@&track_user_id=7891151&v=1",(long)nextPage,string,str,str1];
    
    
    [SSNetworking ssGETWithURL:url parameter:nil success:^(id result) {
        [[YNDataReading shareYNataReading] writeCachePlist:@"traveller" result:result];
        NSDictionary *dic = [[YNDataReading shareYNataReading] readCachePlist:@"traveller"];
        NSArray *dataArray = [dic objectForKey:@"data"];
      
        if (self.isUpLoading == NO) {
            [self.travellerArray removeAllObjects];
            
        }
        
        
        for (NSDictionary *dic in dataArray) {
            TravellerModel *traveller = [[TravellerModel alloc] init];
            
            [traveller setValuesForKeysWithDictionary:dic];
            [self.travellerArray addObject:traveller];
            
        }
        
        [self.traTableView.mj_header endRefreshing];
        [self.traTableView.mj_footer endRefreshing];
        //隐藏
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        [self.traTableView reloadData];

        
    } failure:^(NSError *error) {
        
        
    } network:^(BOOL networking) {
        
        NSDictionary *dic = [[YNDataReading shareYNataReading] readCachePlist:@"traveller"];
        NSArray *dataArray = [dic objectForKey:@"data"];
        
        if (self.isUpLoading == NO) {
            [self.travellerArray removeAllObjects];
            
        }
        
        for (NSDictionary *dic in dataArray) {
            TravellerModel *traveller = [[TravellerModel alloc] init];
            
            [traveller setValuesForKeysWithDictionary:dic];
            [self.travellerArray addObject:traveller];
            
        }
        
        [self.traTableView.mj_header endRefreshing];
        [self.traTableView.mj_footer endRefreshing];
        //隐藏
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        [self.traTableView reloadData];
    }];

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.travellerArray.count;
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TravellerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TravellerCell];
    TravellerModel *model = [self.travellerArray objectAtIndex:indexPath.row];
    cell.traveller = model;
    return cell;
}



//cell数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.timersArray.count;
    
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TimerCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"discountCell" forIndexPath:indexPath];
    
   // item.backgroundColor = [UIColor purpleColor];
    item.label.text = self.timersArray[indexPath.row];
    
    return item;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
   
    
  
}


@end
